package com.pioneer.controller.manager;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.manager.domain.ManGoods;
import com.pioneer.web.manager.service.IManGoodsService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 通知公告
 *
 * @author hlm
 */
@RestController
@RequestMapping("/manager/goods_m")
public class ManGoodsMController extends BaseController {

    @Resource
    private IManGoodsService goodsService;

    /**
     * 获取通知公告列表
     *
     * @param goods 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('manager:goods_m:list')")
    @GetMapping("/list")
    public AjaxResult list(ManGoods goods) {
        boolean isPage = startPage();
        List<ManGoods> list = goodsService.list(Wrappers.query(goods));
        return success(isPage, list);
    }

    /**
     * 根据通知公告id获取详细信息
     *
     * @param goodsId 通知公告id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('manager:goods_m:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable Long goodsId) {
        return AjaxResult.success(goodsService.getById(goodsId));
    }

    /**
     * 新增通知公告
     *
     * @param goods 通知公告
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('manager:goods_m:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ManGoods goods) {
        return toAjax(goodsService.save(goods));
    }

    /**
     * 修改通知公告
     *
     * @param goods 通知公告
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('manager:goods_m:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ManGoods goods) {
        return toAjax(goodsService.updateById(goods));
    }

    /**
     * 删除通知公告
     *
     * @param goodsIds 通知公告id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('manager:goods_m:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds) {
        return toAjax(goodsService.removeByIds(Arrays.asList(goodsIds)));
    }
}
